package com.hrb.douban.fragment;


import android.app.Service;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.hrb.douban.R;
import com.hrb.douban.activtity.CollectPageActivity;
import com.hrb.douban.activtity.PersonInfoActivity;
import com.hrb.douban.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends Fragment implements View.OnClickListener {

    @BindView(R.id.mine_btn_collect)
    ImageView mineBtnCollect;
    @BindView(R.id.mine_btn_history)
    ImageView mineBtnHistory;
    @BindView(R.id.mine_btn_shake)
    ImageView mineBtnShake;
    @BindView(R.id.mine_btn_gallery)
    ImageView mineBtnGallery;
    @BindView(R.id.mine_btn_setting)
    ImageView mineBtnSetting;
    Unbinder unbinder;
    //传感器管理器
    private SensorManager sensorManager;
    private Vibrator vibrator;
    private MediaPlayer mediaPlayer;


    public MineFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        unbinder = ButterKnife.bind(this, view);
        sensorManager = (SensorManager) getActivity().getSystemService(Service.SENSOR_SERVICE);
        vibrator = (Vibrator) getActivity().getSystemService(Service.VIBRATOR_SERVICE);
        // Inflate the layout for this fragment
        mediaPlayer = MediaPlayer.create(getActivity(), Uri.parse("https://static.sesine.com/wechat-weapp-movie/sound/shake.mp3"));
        //mediaPlayer.start();
        sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
        unbinder = ButterKnife.bind(this, view);
        mineBtnCollect.setOnClickListener(this);
        mineBtnHistory.setOnClickListener(this);
        mineBtnShake.setOnClickListener(this);
        mineBtnGallery.setOnClickListener(this);
        mineBtnSetting.setOnClickListener(this);
        return view;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            //相当于Fragment的onResume，为true时，Fragment已经可见
            //mediaPlayer.start();
            if (sensorManager != null) {// 注册监听器
                sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
                // 第一个参数是Listener，第二个参数是所得传感器类型，第三个参数值获取传感器信息的频率
            }
        } else {
            //相当于Fragment的onPause，为false时，Fragment不可见
            if (sensorManager != null) {// 取消监听器
                sensorManager.unregisterListener(sensorEventListener);
            }
        }
    }

    private SensorEventListener sensorEventListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
            float[] values = event.values;
            float x = values[0];
            float y = values[1];
            float z = values[2];
            int medumValue = 19;
            if (Math.abs(x) > medumValue || Math.abs(y) > medumValue || Math.abs(z) > medumValue) {
                vibrator.vibrate(200);
                mediaPlayer.start();
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()){
            case R.id.mine_btn_collect:
                intent=new Intent(getContext(),CollectPageActivity.class);
                intent.putExtra("title","收藏");
                startActivity(intent);
                break;
            case R.id.mine_btn_history:
                intent=new Intent(getContext(),CollectPageActivity.class);
                intent.putExtra("title","浏览记录");
                startActivity(intent);
                break;
            case R.id.mine_btn_shake:

                break;
            case R.id.mine_btn_gallery:
                break;
            case R.id.mine_btn_setting:
                intent=new Intent(getContext(),PersonInfoActivity.class);
                intent.putExtra("title","设置");
                startActivity(intent);
                break;
        }
    }
}
